#include <iostream>
#include <string>
#include <memory>

using std::cout;
using std::endl;
using std::string;
using std::unique_ptr;

//提出薪资申请的人
class RaiseRequest
{
public:
    RaiseRequest(const string &name, int salary)
    : _name(name)
    , _salary(salary)
    {
    }

    string getName() const
    {
        return _name;
    }

    int getSalary() const
    {
        return _salary;
    }
private:
    string _name;
    int _salary;
};

class SalaryApprover
{
public:
    SalaryApprover()
    : _pNext(nullptr)
    {
    }

    //设置下一个处理者
    void setNextChain(SalaryApprover *pNext)
    {
        _pNext = pNext;
    }

    //发送请求给下一个处理者
    void sendRequestToNextHandler(const RaiseRequest &request)
    {
        if(_pNext)
        {
            _pNext->processRequest(request);//多态
        }
        else
        {
            cout << request.getName() << "的加薪请求, 加薪: "
                 << request.getSalary() << "元没有人可以处理!!!"
                 << endl;
        }
    }

    //纯虚函数，真正的处理操作
    virtual void processRequest(const RaiseRequest &request) = 0;

private:
    SalaryApprover *_pNext;
};

class DepartmentManager
: public SalaryApprover
{
public:
    virtual void processRequest(const RaiseRequest &request) override
    {
        //获取请求者的薪资请求
        int salary = request.getSalary();
        if(salary <= 1000)
        {
            cout << request.getName() << "的加薪请求，加薪： "
                 << salary << "元被部门经理通过了" << endl;
        }
        else
        {
            sendRequestToNextHandler(request);
        }
    }
};

class CTO
: public SalaryApprover
{
public:
    virtual void processRequest(const RaiseRequest &request) override
    {
        //获取请求者的薪资请求
        int salary = request.getSalary();
        if(salary > 1000 && salary <= 5000)
        {
            cout << request.getName() << "的加薪请求，加薪： "
                 << salary << "元被CTO通过了" << endl;
        }
        else
        {
            sendRequestToNextHandler(request);
        }
    }
};

class GeneralManager
: public SalaryApprover
{
public:
    virtual void processRequest(const RaiseRequest &request) override
    {
        //获取请求者的薪资请求
        int salary = request.getSalary();
        if(salary > 5000 && salary < 30000)
        {
            cout << request.getName() << "的加薪请求，加薪： "
                 << salary << "元被总经理通过了" << endl;
        }
        else
        {
            sendRequestToNextHandler(request);
        }
    }
};

void test()
{
    //创建三个领导对象
    unique_ptr<SalaryApprover> pdm(new DepartmentManager());
    unique_ptr<SalaryApprover> pcto(new CTO());
    unique_ptr<SalaryApprover> pgm(new GeneralManager());

    //创建申请加薪的同事
    RaiseRequest zhangsan("张三", 300);
    RaiseRequest lisi("李四", 4000);
    RaiseRequest wangwu("王五", 20000);
    RaiseRequest shiwan("十万", 100000);

    //设置领导之间的关系
    pdm->setNextChain(pcto.get());
    pcto->setNextChain(pgm.get());
    pgm->setNextChain(nullptr);

    //四个申请者申请加薪操作
    pdm->processRequest(zhangsan);
    pdm->processRequest(lisi);
    pdm->processRequest(wangwu);
    pdm->processRequest(shiwan);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}


